<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>图片切换</title>
    <style type="text/css">
        *{
            margin:0;
            padding: 0;
        }
        #outer{
            width:500px;
            margin: 50px auto;
            padding: 10px;
            background-color: aqua;
            text-align: center;
        }
    </style>
    <script>
        window.onload=function(){
            var prev=document.getElementById("prev");
            var next=document.getElementById("next");
            //获取img标签
            var img=document.getElementsByTagName("img")[0];
            var imgArr=["1.png","2.png","3.png"];
            //创建一个变量保存当前显示的图片的索引
            var index=0;

            prev.onclick=function (){
                index--;
                if(index<0) {
                    index=imgArr.length-1;
                }
                img.src=imgArr[index];
            };
            next.onclick=function (){
                index++;
                if(index>imgArr.length){
                    index=0;
                }
                img.src=imgArr[index];
            };
        }
    </script>
</head>
<body>
<div id="outer">
    <img src="1.png" alt="111"/>

    <button id="prev">上一张</button>
    <button id="next">下一张</button>
</div>
</body>
</html>